package com.hczt.fire.server.backend.sys.service.impl;

import com.hczt.fire.entity.sys.SysRole;
import com.hczt.fire.entity.sys.SysUser;
import com.hczt.fire.entity.sys.SysUser_;
import com.hczt.fire.framework.jpa.repository.IBaseDao;
import com.hczt.fire.framework.jpa.repository.support.PagedQuery;
import com.hczt.fire.framework.jpa.repository.support.QueryCriteria;
import com.hczt.fire.framework.jpa.repository.support.QueryEnum;
import com.hczt.fire.server.backend.sys.service.SysUserService;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * いま 最高の表現 として 明日最新の始発．．～
 * Today the best performance  as tomorrow newest starter!
 *
 * @author marvin.ma
 * @create 2017-10-20 13:42
 * @desc 系统用户
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Resource
    private IBaseDao baseDao;

    /**
     * @param pageNumber   当前页码
     * @param pageSize  每页显示条数
     * @param username  用户名
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @throws Exception
     */
    @Override
    public PagedQuery<SysUser> findPage(Integer pageNumber, Integer pageSize, String username, String startTime, String endTime) throws Exception {
        PagedQuery<SysUser> pagedQuery = new PagedQuery<>();
        pagedQuery.setPageNumber(pageNumber);
        pagedQuery.setPageSize(pageSize);
        if (StrUtil.isNotEmpty(username)) {
            pagedQuery.addWhereCondition(SysUser_.username.toString(), QueryEnum.EQUAL, username);
        }
        if (StrUtil.isNotEmpty(startTime)) {
            pagedQuery.addWhereCondition(SysUser_.modifyTime.toString(), QueryEnum.GREATER_EQUAL_THAN, startTime);
        }
        if (StrUtil.isNotEmpty(endTime)) {
            pagedQuery.addWhereCondition(SysUser_.username.toString(), QueryEnum.LESS_EQUAL_THAN, endTime);
        }

        //pagedQuery.addOrderBy(SysUser_.createTime.toString(), Order);

        pagedQuery = baseDao.findBy(pagedQuery, SysUser.class);
        return pagedQuery;
    }

    /**
     * 根据登录账号查找用户
     *
     * @param userName
     */
    @Override
    public SysUser findByUserName(String userName) {
        QueryCriteria<SysUser, SysUser> criteria = baseDao.getCriteriaQuery(SysUser.class, SysUser.class);
        criteria.getQuery().where(criteria.getBuilder().equal(criteria.getEntity().get(SysUser_.username), userName));
        List<SysUser> userList = baseDao.findListByCriteriaQuery(criteria.getQuery());
        //List<SysUser> userList = baseDao.findListByProperty(SysUser.class, "userName", userName);
        if(null != userList && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    /**
     * 保存用户信息和关联用户和角色
     *
     * @param user   用户对象
     * @param roleId 角色ID
     */
    @Override
    public Boolean saveUserAndUserRole(SysUser user, Long roleId) throws Exception {
        int count = 0;
        //加密
        user.setPassword(SecureUtil.md5().digestHex(user.getPassword()));
        user.setIsLock(false);
        user.setIsDel(false);
        SysRole role = baseDao.findById(SysRole.class, roleId);
        if (SysRole.ROLE_TYPE.equals(role.getPerms())) {
            user.setIsAdmin(true);
        } else {
            user.setIsAdmin(false);
        }
        baseDao.save(user);
        //关联用户和角色信息
        //SysUserRole userRole = new SysUserRole();
        //userRole.setRoleId(roleId);
        //userRole.setUserId(user.getId());
        //userRole.setCreateTime(user.getCreateTime());
        //userRole.setModifyTime(user.getCreateTime());
        //count = userRoleMapper.insert(userRole);
        return true;
    }
}
